<template>
  <div class="container">
    <div class="login">
      <el-row type="flex" justify="center" align="middle">
        <el-col :xs="24" :sm="13" :md="13" :lg="13" :xl="13">
          <img class="loginImg" src="../assets/images/login.png" />
        </el-col>
        <el-col :xs="24" :sm="9" :md="9" :lg="9" :xl="9">
          <div class="loginForm">
            <el-form
              ref="loginForm"
              class="user-layout-login"
              :model="loginForm"
              :rules="loginRules"
            >
              <div class="logoContent">
                <img src="@/assets/logo/logo.png" class="logo" alt="logo" />
                <div style="font-size: 32px">{{ title }}</div>
              </div>
              <el-form-item prop="username">
                <el-input v-model="loginForm.username" size="large" placeholder="账户">
                  <svg-icon
                    slot="prefix"
                    icon-class="用户"
                    class="el-input__icon input-icon"
                  />
                </el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input
                  v-model="loginForm.password"
                  size="large"
                  placeholder="密码"
                  show-password
                >
                  <svg-icon
                    slot="prefix"
                    icon-class="锁"
                    class="el-input__icon input-icon"
                  />
                </el-input>
              </el-form-item>
              <el-row :gutter="16" v-if="captchaEnabled">
                <el-col class="gutter-row" :span="16">
                  <el-form-item prop="code">
                    <el-input
                      v-model="loginForm.code"
                      size="large"
                      type="text"
                      autocomplete="off"
                      placeholder="验证码"
                    >
                      <svg-icon
                        slot="prefix"
                        icon-class="安全"
                        class="el-input__icon input-icon"
                      />
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col class="gutter-row" :span="8">
                  <img class="getCaptcha" :src="codeUrl" @click="getCode" />
                </el-col>
              </el-row>
              <el-form-item>
                <div style="height: 19px; line-height: 19px">
                  <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
                </div>
              </el-form-item>
              <el-form-item style="margin-top: 24px">
                <el-button
                  size="large"
                  type="primary"
                  htmlType="submit"
                  class="login-button"
                  :loading="loading"
                  :disabled="loading"
                  @click="handleLogin"
                  >确 定</el-button
                >
              </el-form-item>
            </el-form>
          </div>
        </el-col>
      </el-row>
    </div>
    <img class="decorateImg" src="@/assets/images/background.svg" />
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";

export default {
  name: "Login",
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
        password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
    window.addEventListener("keydown", this.keyDown, true);
  },
  methods: {
    keyDown(e) {
      if (e.keyCode == 13) {
        this.handleLogin();
      }
    },
    getCode() {
      getCodeImg().then((res) => {
        this.captchaEnabled =
          res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.data.img;
          this.loginForm.uuid = res.data.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set("rememberMe", this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store.state.tagsView.visitedViews = [];
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              window.removeEventListener("keydown", this.keyDown, false);
              this.$router.push({ path: this.redirect || "/" }).catch(() => {});
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: 100%;
  width: 100%;
  padding: 10px;
  display: flex;
  vertical-align: middle;
  background-size: cover;
  background-image: url("../assets/images/login-back.jpg");
}

.decorateImg {
  position: absolute;
  height: calc(100% - 10px);
  width: calc(100% - 10px);
}

.login {
  margin: auto;
  padding: 10px;
  background: #ffffff;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
  border-radius: 8px;
  z-index: 999;

  .loginImg {
    height: 100%;
    width: 100%;
    padding: 30px;
  }

  .loginForm {
    height: 500px;
    padding: 0 30px;
    border-left: 1px solid #cccccc5e;

    .logoContent {
      display: flex;
      align-items: center;
      padding: 40px 0;
    }
    .logo {
      width: 140px;
      height: 60px;
      padding: 5px;
      margin-right: 10px;
      vertical-align: middle;
    }
    h1 {
      font-family: montserrat;
      color: #8d8d8d !important;
      margin-left: 20px;
    }

    .footer-text {
      font-size: 14px;
      height: 19px;
      line-height: 19px;
      span {
        color: #bbbbbb;
      }
      .el-link {
        margin-left: 20px !important;
      }
    }
  }
}

.el-row--flex {
  flex-flow: row wrap;
}

.QRcodeIcon {
  position: absolute;
  right: 0;
  font-size: 32px;
  color: #2f54eb;
  margin-top: 5px;
  margin-right: 5px;
}
.ant-space-align-center {
  color: #8f959e;
  line-height: 30px;
  height: 30px;
}

.user-layout-login label {
  font-size: 12px !important;
}

.el-checkbox__input.is-checked + .el-checkbox__label {
  color: #000000 !important;
}

.user-layout-login {
  border-left: #8c8c8c 3px;

  label {
    font-size: 14px;
  }
  .ant-page-header {
    padding: 60px 0 45px 0;
  }
  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
    border-radius: 4px;
  }
  .icons-list {
    .anticon {
      font-size: 30px;
      width: 40px;
    }
    .dingtalk {
      color: #0089ff;
    }
    .WeChat {
      color: #51c332;
    }
    .Alipay {
      color: #06b4fd;
    }
    .Sina {
      color: #d81e06;
    }
  }
}
</style>
